package com.lakeworks.common.validation;

import com.lakeworks.common.annotation.EnumValue;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import static java.util.Arrays.stream;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

/**
 * @Author: Auxps
 * @Date: 2023/7/14
 * Description:
 */
public class EnumValuesValidator implements ConstraintValidator<EnumValue, String> {

    private String[] strValues;

    @Override
    public void initialize(EnumValue constraintAnnotation) {
        strValues = constraintAnnotation.strValues();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        return isNotBlank(s) && stream(strValues).anyMatch(s::equals);
    }
}
